package com.tools.cleanmaster.ui.notification.item;

import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.text.Html;
import android.widget.RemoteViews;

import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.scenenew.BatterySaverHelper;
import com.tools.cleanmaster.scenenew.SceneConstants;
import com.tools.cleanmaster.scenenew.SceneType;
import com.tools.cleanmaster.store.SceneSharedPrefManager;
import com.tools.cleanmaster.ui.activity.LandingPageGuideActivity;
import com.tools.cleanmaster.ui.notification.NotificationBaseItem;
import com.tools.cleanmaster.ui.notification.UIBuilder;
import com.tools.cleanmaster.ui.notification.UIType;
import com.tools.cleanmaster.utils.FeatureConfig;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 短时间内耗电过快
 */
public class BatterySharpDecItem extends NotificationBaseItem {
    public static final String JSON_KEY_SWITCH = "switch";
    public static final String JSON_KEY_INTERVAL = "interval";
    public static final String JSON_KEY_TIME = "time";
    public static final String JSON_KEY_PERCENT = "percent";

    public static final boolean DEFAULT_SWITCH = true;
    public static final int DEFAULT_INTERVAL_HOUR = 12;
    public static final int DEFAULT_TIME_MIN = 10;
    public static final int DEFAULT_PERCENT = 3;

    private boolean mSwitch = DEFAULT_SWITCH;
    private int mIntervalHour = DEFAULT_INTERVAL_HOUR;
    private int mTimeMin = DEFAULT_TIME_MIN;
    private int mPercent = DEFAULT_PERCENT;

    public BatterySharpDecItem() {
        super();
        String jsonBody = SceneSharedPrefManager.getSingleSceneConfigData(getSceneType());
        try {
            JSONObject jsonObject = new JSONObject(jsonBody);
            mSwitch = jsonObject.optBoolean(JSON_KEY_SWITCH, DEFAULT_SWITCH);
            mIntervalHour = jsonObject.optInt(JSON_KEY_INTERVAL, DEFAULT_INTERVAL_HOUR);
            mTimeMin = jsonObject.optInt(JSON_KEY_TIME, DEFAULT_TIME_MIN);
            mPercent = jsonObject.optInt(JSON_KEY_PERCENT, DEFAULT_PERCENT);
        } catch (JSONException e) {
            if (FeatureConfig.DEBUG_LOG) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public boolean isSceneValid() {
        if (!mSwitch) {
            if (FeatureConfig.DEBUG_LOG) {
                log("开关关闭，无法展示");
            }
            return false;
        }
        Context appContext = CleanApplication.getInstance();
        ResolveInfo resolveInfo = null;
        try {
            resolveInfo = BatterySaverHelper.getInstalledBatterySaverInfo(appContext);
        } catch (Exception e) {
        }
        if (resolveInfo != null) {
            if (FeatureConfig.DEBUG_LOG) {
                log("省电已经安装，不展示");
            }
            return false;
        }
        return true;
    }

    @Override
    public Notification prepareNotification() {
        Context ctx = CleanApplication.getInstance();
        Intent intent;

        intent = new Intent(ctx, LandingPageGuideActivity.class);
        intent.putExtra(SCENE_TYPE_EXTRA_FLAG, SceneType.BAT_SHARPDEC.key);
        intent.putExtra(SceneConstants.EXTRA_FROM_KEY, SceneConstants.EXTRA_FROM_VALUE_STATUS_BAR);

        UIParam param = new UIParam();
        param.viewType = UIType.TITLE_WITH_CONTENT_GREEN_BTN;
        param.sceneType = getSceneType();
        param.tickerText = Html.fromHtml(ctx.getString(R.string.battery_sharpdec_title));
        param.title = Html.fromHtml(ctx.getString(R.string.battery_sharpdec_title));
        param.btnText = ctx.getString(R.string.battery_low_btn);
        param.content = Html.fromHtml(ctx.getString(R.string.diagnostic_active_content));
        param.contentIntent = intent;
        param.leftIconResId = R.drawable.ic_scene_power_consumption;
        param.tickerIcon = R.drawable.ic_notify_scene_power_consumption;
        UIBuilder builder = new UIBuilder(param);
        Notification notification = builder.build();
        RemoteViews views = notification.contentView;
        views.setTextViewText(R.id.notification_content, param.content);
        views.setTextViewText(R.id.notification_button, param.btnText);
        return notification;
    }

    @Override
    public SceneType getSceneType() {
        return SceneType.BAT_SHARPDEC;
    }

    @Override
    public boolean isTimeValid() {
        long lastShow = SceneSharedPrefManager.getLastSceneShowTime(getSceneType());
        long now = System.currentTimeMillis();
        if (now - lastShow < mIntervalHour * SceneConstants.HOUR_MS) {
            if (FeatureConfig.DEBUG_LOG) {
                log("距离上次展示小于 " + mIntervalHour + " 小时，无法展示");
            }
            return false;
        }
        return true;
    }
}
